package com.dstation.controller;


import com.alibaba.fastjson.JSONObject;
import com.dstation.domain.UserInfo;
import com.dstation.domain.VideoCommentInfo;
import com.dstation.service.interf.IVideoCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

@Controller
@RequestMapping(value = "/videoComment")
public class VideoCommentController {

    @Autowired
    IVideoCommentService videoCommentService;

    /**
     * 发表评论
     * @param videoId
     * @param commentTxt
     * @param session
     * @param preCommentId  0代表父评论ID，没有的话则为0
     * @return
     */
    @RequestMapping("/publicComment")
    @ResponseBody
    public JSONObject publicComment(@RequestParam("videoId") Long videoId, @RequestParam("commentTxt") String commentTxt,
                                    @RequestParam("preCommentId") Long preCommentId, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        VideoCommentInfo videoCommentInfo = new VideoCommentInfo();
        videoCommentInfo.setUserId(((UserInfo)session.getAttribute("userInfo")).getUserId());
        videoCommentInfo.setVideoId(videoId);
        videoCommentInfo.setCommentContent(commentTxt);
        videoCommentInfo.setPreCommentId(preCommentId);
        videoCommentInfo.setCreateDate(new Timestamp(System.currentTimeMillis()));
        boolean saveRes = videoCommentService.addCommentInfo(videoCommentInfo);
        jsonObject.put("saveRes", saveRes);
        return jsonObject;
    }
}
